/**
* 版权声明：厦门睿商网络科技有限公司 版权所有 违者必究
* 日    期：2020-05-13
*/
package com.rzico.basics.entity;

import lombok.Data;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import javax.persistence.*;
import io.swagger.annotations.ApiModelProperty;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

/**
 * <pre>
 *   分销设置数据库对象
 * </pre>
 * @author Rzico Boot
 * @version 1.0
 */
@Data
@JsonIgnoreProperties(value = {"handler"})
@JsonInclude(JsonInclude.Include.NON_NULL)
@Table(name = "wx_distribution")
public class Distribution {

    @Id
    @ApiModelProperty(value = "" )
    protected Long id;

    @ApiModelProperty(value = "创建日期" )
    protected java.util.Date createDate;

    @ApiModelProperty(value = "修改日期" )
    protected java.util.Date modifyDate;

    @ApiModelProperty(value = "名称" )
    protected String name;

    @ApiModelProperty(value = "一级佣金" )
    protected java.math.BigDecimal percent1;

    @ApiModelProperty(value = "二级佣金" )
    protected java.math.BigDecimal percent2;

    @ApiModelProperty(value = "同级佣金" )
    protected java.math.BigDecimal peerRate;

    @ApiModelProperty(value = "代理佣金" )
    protected java.math.BigDecimal agent1;

    @ApiModelProperty(value = "复购佣金" )
    protected java.math.BigDecimal againRate;

    @ApiModelProperty(value = "基金比例" )
    protected java.math.BigDecimal bonusRate;

    @ApiModelProperty(value = "赠送积分" )
    protected java.math.BigDecimal given;

    @ApiModelProperty(value = "是否统计绩效" )
    protected Boolean isAchieve;

    @ApiModelProperty(value = "" )
    protected Integer orders;

    @ApiModelProperty(value = "类型 {0:默认设置,1.单品设置}" )
    protected Integer type;

    @ApiModelProperty(value = "状态 {0:启用,1.关闭}" )
    protected Integer status;

    @ApiModelProperty(value = "所属企业" )
    protected Long enterpriseId;

    protected List<DistributionItem> itemList = Collections.emptyList();

    @JsonIgnore
    public BigDecimal getVPercent1(Vip vip) {
        if (vip!=null) {
            for (DistributionItem item : getItemList()) {
                if (item.getVipId().equals(vip.getId())) {
                    return item.getPercent1();
                }
            }
        }
        return getPercent1();
    }

    @JsonIgnore
    public BigDecimal getVPercent2(Vip vip) {
        if (vip!=null) {
            for (DistributionItem item : getItemList()) {
                if (item.getVipId().equals(vip.getId())) {
                    return item.getPercent2();
                }
            }
        }
        return getPercent2();
    }

    @JsonIgnore
    public BigDecimal getVFund(Vip vip) {
        if (vip!=null) {
            for (DistributionItem item : getItemList()) {
                if (item.getVipId().equals(vip.getId())) {
                    return item.getBonusRate();
                }
            }
        }
        return BigDecimal.ZERO;
    }

    @JsonIgnore
    public Boolean getVWarehouseCoupon(Vip vip) {
        if (vip!=null) {
            for (DistributionItem item : getItemList()) {
                if (item.getVipId().equals(vip.getId())) {
                    return item.getWarehouseCoupon();
                }
            }
        }
        return false;
    }

    @JsonIgnore
    public BigDecimal getVWarehouseRate(Vip vip) {
        if (vip!=null) {
            for (DistributionItem item : getItemList()) {
                if (item.getVipId().equals(vip.getId())) {
                    return item.getWarehouseRate();
                }
            }
        }
        return BigDecimal.ZERO;
    }

    @JsonIgnore
    public BigDecimal getVPeerRate(Vip vip) {
        BigDecimal peerRate = BigDecimal.ZERO;
        if (vip!=null) {
            for (DistributionItem item : getItemList()) {
                if (item.getVipId().equals(vip.getId())) {
                    peerRate = item.getPeerRate();
                }
            }
        }
        if (peerRate.compareTo(BigDecimal.ZERO)==0) {
            return getVPercent2(vip);
        } else {
            return peerRate;
        }
    }

    @JsonIgnore
    public BigDecimal getVAgent1(Vip vip) {
        if (vip!=null) {
            for (DistributionItem item : getItemList()) {
                if (item.getVipId().equals(vip.getId())) {
                    return item.getAgent1();
                }
            }
        }
        return getAgent1();
    }

}
